<?php

namespace app\admin\model;

use app\admin\model\project\Income;
use think\Model;

/**
 * Other
 */
class Statistics extends Model
{
    // 表名
    public function tongji(){
        $data = request()->post();  //应用ID

        $start_date = $data[0] ?? '';
        $end_date = $data[1] ?? '';
        $data['taobao'] = $this->getTabBao($start_date,$end_date)[0] ?? [];//淘宝刷单
        $data['channel'] = $this->channelAmount($start_date,$end_date)[0] ?? [];//渠道收入
        $data['other'] = $this->otherCost($start_date,$end_date)[0] ?? [];//其他支出
        $data['maint'] = $this->maintAmount($start_date,$end_date)[0] ?? [];//其他支出
        $data['total_amount'] = $this->totalAmount($start_date,$end_date)[0] ?? [];//项目总收入
        $data['total_income'] = $this->totalIncome($start_date,$end_date)[0] ?? [];//项目实际到账
        $data['custom_nums'] = $this->customNums($start_date,$end_date);//客户数量
        $data['project_nums'] = $this->projectNums($start_date,$end_date);//项目总数量
        return $data;
    }


    public function getOtherPay(){
        // 获取当前年份
        $year = request()->post('year');
        $startOfYear = strtotime("{$year}-01-01 00:00:00");
        $endOfYear = strtotime("{$year}-12-31 23:59:59");

        $map[] = ['date','>=',$startOfYear];
        $map[] = ['date','<=',$endOfYear];
        $month = (new Other())
            ->field(['SUM(money) as total', 'DATE_FORMAT(FROM_UNIXTIME(date), "%m") as month'])
            ->where($map)  // 仅查询当年的数据
            ->group('month')  // 按月份分组
            ->order('month')  // 按月份排序
            ->select();

        $year = (new Other())
            ->field(['SUM(money) as total', 'DATE_FORMAT(FROM_UNIXTIME(date), "%Y") as year'])
            ->group('year')  // 按月份分组
            ->order('year desc')  // 按月份排序
            ->select();

        return ['month' => $month,'year' => $year];
    }


    public function getChannelIncome(){
        // 获取当前年份
        $year = request()->post('year');
        $startOfYear = strtotime("{$year}-01-01 00:00:00");
        $endOfYear = strtotime("{$year}-12-31 23:59:59");

        $map[] = ['date','>=',$startOfYear];
        $map[] = ['date','<=',$endOfYear];
        $month = (new Channel())
            ->field(['SUM(income) as total', 'DATE_FORMAT(FROM_UNIXTIME(date), "%m") as month'])
            ->where($map)  // 仅查询当年的数据
            ->group('month')  // 按月份分组
            ->order('month')  // 按月份排序
            ->select();

        $year = (new Channel())
            ->field(['SUM(income) as total', 'DATE_FORMAT(FROM_UNIXTIME(date), "%Y") as year'])
            ->group('year')  // 按月份分组
            ->order('year desc')  // 按月份排序
            ->select();

        return ['month' => $month,'year' => $year];

    }




    public function getProjectInCome(){
        // 获取当前年份
        $year = request()->post('year');
        $startOfYear = strtotime("{$year}-01-01 00:00:00");
        $endOfYear = strtotime("{$year}-12-31 23:59:59");

        $map[] = ['date','>=',$startOfYear];
        $map[] = ['date','<=',$endOfYear];
        $month = (new Income())
            ->field(['SUM(money) as total', 'DATE_FORMAT(FROM_UNIXTIME(date), "%m") as month'])
            ->where($map)  // 仅查询当年的数据
            ->group('month')  // 按月份分组
            ->order('month')  // 按月份排序
            ->select();

        $year = (new Income())
            ->field(['SUM(money) as total', 'DATE_FORMAT(FROM_UNIXTIME(date), "%Y") as year'])
            ->group('year')  // 按月份分组
            ->order('year desc')  // 按月份排序
            ->select();


        return ['month' => $month,'year' => $year];
    }




    public function projectNums($start_date,$end_date){
        $map = [];
        if($start_date && $end_date){
            $map[] = ['create_time', 'between', [strtotime($start_date), strtotime($end_date)]];
        }
        return (new Project())->where($map)->count();
    }

    public function customNums($start_date,$end_date){
        $map = [];
        if($start_date && $end_date){
            $map[] = ['create_time', 'between', [strtotime($start_date), strtotime($end_date)]];
        }
        return (new Custom())->where($map)->count();
    }

    public function totalIncome($start_date,$end_date){
        $map = [];
        if($start_date && $end_date){
            $map[] = ['date', 'between', [strtotime($start_date), strtotime($end_date)]];
        }
        return (new Income())->where($map)->field('SUM(money) as money,count(id) as count')->select();
    }

    public function totalAmount($start_date,$end_date){
        $map = [];
        if($start_date && $end_date){
            $map[] = ['create_time', 'between', [strtotime($start_date), strtotime($end_date)]];
        }
        return (new Project())->where($map)->field('SUM(total_money) as total_money,count(id) as count')->select();
    }


    public function maintAmount($start_date,$end_date){
        $map = [];
        if($start_date && $end_date){
            $map[] = ['date', 'between', [$start_date, $end_date]];
        }
        return (new Maint())->where($map)->field('SUM(money) as money,count(id) as count')->select();
    }

    public function otherCost($start_date,$end_date){
        $map = [];
        if($start_date && $end_date){
            $map[] = ['date', 'between',[strtotime($start_date), strtotime($end_date)]];
        }
        return (new Other())->where($map)->field('SUM(money) as money,count(id) as count')->select();
    }


    public function channelAmount($start_date,$end_date){
        $map = [];
        if($start_date && $end_date){
            $map[] = ['date', 'between',[strtotime($start_date), strtotime($end_date)]];
        }
        return (new Channel())->where($map)->field('SUM(income) as income,count(id) as count')->select();
    }
    public function getTabBao($start_date,$end_date){
        $map = [];
        if($start_date && $end_date){
            $map[] = ['date', 'between', [strtotime($start_date), strtotime($end_date)]];
        }
        $res = (new Taobao())->where($map)->field('SUM(total_money) as total_money,count(id) as count')->select();
        return $res;
    }

}
